package cn.tedu.eb_film.service.impl;

import cn.tedu.eb_film.ex.ServiceException;
import cn.tedu.eb_film.mapper.EmployeeMapper;
import cn.tedu.eb_film.pojo.dto.EmployeeAddNewDTO;
import cn.tedu.eb_film.pojo.dto.EmployeeUpdateDTO;
import cn.tedu.eb_film.pojo.entity.Employee;
import cn.tedu.eb_film.pojo.vo.*;
import cn.tedu.eb_film.service.EmployeeService;
import cn.tedu.eb_film.utils.ServiceCode;
import cn.tedu.eb_film.utils.StateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工表服务实现类
 *
 * @author zzzzc
 * @since 2023-03-18 18:38:25
 */
@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    public EmployeeServiceImpl(){
        log.debug("创建业务类对象：EmployeeServiceImpl");
    }

    @Override
    public void addNew(EmployeeAddNewDTO employeeAddNewDTO) {
        log.debug("开始处理【添加员工】的业务，参数:{}",employeeAddNewDTO);
        String name = employeeAddNewDTO.getName();
        int count = employeeMapper.countByName(name);
        log.debug("根据名字【{}】统计数量，结果:{}",name,count);
        if (count>0){
            String message = "添加失败，名字已存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeAddNewDTO,employee);
        log.debug("即将执行插入数据，参数：{}",employee);
        employeeMapper.insert(employee);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除员工】的业务，参数：{}", id);
        EmployeeStandardVO employeeStandardVO = employeeMapper.getStandardById(id);
        log.debug("根据ID={}检查询员工数据是否存在，查询结果：{}", id, employeeStandardVO);
        if (employeeStandardVO == null || employeeStandardVO.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "删除失败，该员工不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        employeeMapper.deleteById(id);

    }

    @Override
    public void setEnable(Long id) {
        updateEnableById(id,1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id,0);
    }

    private void updateEnableById(Long id,Integer enable) {
        EmployeeStandardVO standardById = employeeMapper.getStandardById(id);
        if (standardById == null||standardById.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = ENABLE_TEXT[enable]+"数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if (enable == standardById.getState()){
            String message = ENABLE_TEXT[enable]+"失败,修改的目标状态与目前一致";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        Employee employee = new Employee();
        employee.setId(id);
        employee.setState(enable);

        int rows = employeeMapper.update(employee);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }

    }

    @Override
    public void updateInfoById(Long id, EmployeeUpdateDTO employeeUpdateDTO) {
        log.debug("开始处理【修改员工详情】的业务，ID：{}，新数据：{}", id, employeeUpdateDTO);
        EmployeeStandardVO standardById = employeeMapper.getStandardById(id);
        if (standardById == null || standardById.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "修改失败，该员工不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int i = employeeMapper.countByNameAndNotId(id, employeeUpdateDTO.getName());
        if (i>0){
            String message = "修改失败，名称被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeUpdateDTO,employee);
        employee.setId(id);
        employeeMapper.update(employee);
    }

    @Override
    public EmployeeStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据员工id查询详情】的业务，ID：{}", id );
        EmployeeStandardVO standardById = employeeMapper.getStandardById(id);
        if (standardById==null||standardById.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "查询失败，数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return standardById;
    }

    @Override
    public List<EmployeeListItemVO> list() {
        List<EmployeeListItemVO> list = employeeMapper.list();
//        list.removeIf(hall -> hall.getState() == StateCode.STATE_DELETE.getValue());
        return list;
    }

    @Override
    public List<EmployeeLikeNameVO> nameList(String likeName) {
        List<EmployeeLikeNameVO> list=employeeMapper.nameList(likeName);
        return list;
    }
}
